<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <link rel="Shortcut Icon" href="../images/aecc.ico" type="image/x-icon" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="../StaticFiles/element-ui/element-ui.css">
  <link rel="stylesheet" href="../StaticFiles/nprogress/nprogress.min.css">
  <link rel="stylesheet" href="../styles/vue-page-common.css">
  <script src="../Scripts/vue.min.js"></script>
  <script src="../StaticFiles/element-ui/element-ui.js"></script>
  <script src="../Scripts/axios.min.js"></script>
  <script src="../StaticFiles/nprogress/nprogress.min.js"></script>
  <script src="../Scripts/dayjs.min.js"></script>
  <script src="../Scripts/axios.config.js"></script>
  <script src="../Scripts/crypto-js.js"></script>
  <script src="../Scripts/secret.js"></script>
  <title>施工单报表</title>
  <style>
    .el-progress__text {
      font-size: 12px !important;
    }
  </style>
</head>

<body>
  <div id="app" v-cloak v-on:keyup.enter="getData">
    <!-- 报表标题 -->
    <vue2-report-header :title="'施工单报表'"></vue2-report-header>
    <div style="padding: 10px;">
      <!-- 查询条件区 -->
      <el-form :inline="true" :model="queryInfo" ref="queryInfoRef" :label-position="'left'">
        <el-form-item label="分厂:" prop="factoryid">
          <el-select v-model="queryInfo.factoryid" placeholder="请选择分厂" clearable filterable
            @change="factorySelectChange">
            <el-option v-for="f in factoryList" :key="f.factoryid" :label="f.factoryname" :value="f.factoryid">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item prop='mfgordername' label="订单号:">
          <el-autocomplete v-model.trim="queryInfo.mfgordername" placeholder="请输入订单号" :trigger-on-focus="false"
            :fetch-suggestions="getMfgordernameAsync" :value-key="'mfgordername'" :debounce="500" style="width:200px;">
          </el-autocomplete>
        </el-form-item>

        <el-form-item prop='engineno' label="发动机号:">
          <el-autocomplete v-model.trim="queryInfo.engineno" placeholder="请输入发动机号" :trigger-on-focus="false"
            :fetch-suggestions="getEngineNoAsync" :value-key="'engineno'" :debounce="500" style="width:200px;">
          </el-autocomplete>
        </el-form-item>

        <el-form-item prop='workflowname' label="施工单号:">
          <el-autocomplete v-model.trim="queryInfo.workflowname" placeholder="请输入施工单号" :trigger-on-focus="false"
            :fetch-suggestions="getSgdNoAsync" :value-key="'workflowname'" :debounce="500" style="width:200px;">
          </el-autocomplete>
        </el-form-item>

        <el-form-item label="机型:">
          <el-input v-model.trim="queryInfo.productfamilyname" placeholder="请输入" clearable style="width: 150px;">
          </el-input>
        </el-form-item>

        <el-form-item label="创建人:">
          <el-input v-model.trim="queryInfo.creater" placeholder="请输入" clearable style="width: 150px;"></el-input>
        </el-form-item>

        <el-form-item label="创建时间:">
          <el-date-picker v-model="queryInfo.createdate" type="daterange" range-separator="至" value-format="yyyy-MM-dd"
            start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions"></el-date-picker>
        </el-form-item>

        <el-form-item label="完工时间:">
          <el-date-picker v-model="queryInfo.finishdate" type="daterange" range-separator="至" value-format="yyyy-MM-dd"
            start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions"></el-date-picker>
        </el-form-item>

        <el-form-item label="生成状态:">
          <el-select v-model="queryInfo.status" placeholder="请选择" clearable filterable style="width: 120px;">
            <el-option :key="'已生成'" :label="'已生成'" :value="'已生成'"></el-option>
            <el-option :key="'待生成'" :label="'待生成'" :value="'待生成'"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="small" @click="getData">查询</el-button>
          <el-button type="primary" icon="el-icon-download" size="small" @click="getExportExcel">导出</el-button>
        </el-form-item>
      </el-form>

      <!-- 表格显示区 -->
      <el-table v-loading="loading" v-loading.fullscreen.lock="loading" :element-loading-text="loadingText"
        element-loading-background="rgba(0,0,0,0.8)" :data="dataList" stripe border>
        <el-table-column type="index" width="50"></el-table-column>
        <el-table-column label="操作" width="90">
          <template slot-scope="scope">
            <el-link type="primary" @click="showDetail(scope.row)">查看明细</el-link>
          </template>
        </el-table-column>
        <el-table-column label="containerid" prop="containerid" v-if="false"></el-table-column>
        <el-table-column label="workflowid" prop="workflowid" v-if="false"></el-table-column>
        <el-table-column label="分厂" prop="factoryname" width="100" show-overflow-tooltip></el-table-column>
        <el-table-column label="施工单号" prop="workflowname" width="200" show-overflow-tooltip></el-table-column>
        <el-table-column label="生成状态" width="80" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-tag v-if="scope.row.containerid !== null" type="success">已生成</el-tag>
            <el-tag v-else type="danger">待生成</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="完成进度" prop="finishrate" width="180">
          <template slot-scope="scope">
            <el-progress :percentage='scope.row.finishrate.split("%")[0]' :stroke-width="8"
              :color="customColorMethod(scope.row.finishrate)" class="red">
            </el-progress>
          </template>
        </el-table-column>
        <el-table-column label="发动机号" prop="engineno" show-overflow-tooltip></el-table-column>
        <el-table-column label="订单号" prop="mfgordername" show-overflow-tooltip></el-table-column>
        <el-table-column label="机型" prop="productfamilyname" width="100" show-overflow-tooltip></el-table-column>
        <el-table-column label="下达时间" prop="xiadadate" width="100">
          <template slot-scope="scope" v-if="scope.row.xiadadate">
            {{scope.row.xiadadate.split(' ')[0]}}
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createdatetime" width="100" :formatter="splitDate"></el-table-column>
        <el-table-column label="完工时间" prop="finishdate" width="120" show-overflow-tooltip>
          <template slot-scope="scope" v-if="scope.row.finishdate">
            <el-tag type="success">{{scope.row.finishdate.split('T')[0]}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建人" prop="fullname" width="80"></el-table-column>
        <el-table-column label="定额总工时" prop="durationperunitsum" width="100" show-overflow-tooltip></el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.currentPage" :page-sizes="[12,50,100]" :page-size="queryInfo.pageSize"
        layout="prev,pager,next,jumper,sizes,total" :total="total">
      </el-pagination>

      <!-- 回到顶部 -->
      <el-backtop></el-backtop>

      <!-- 明细 -->
      <el-dialog :title="detail.title" :width="'98%'" :top="'10px'" :visible.sync="detail.dialogVisiable"
        :center="true">
        <el-table v-loading="loading" v-loading.fullscreen.lock="loading" :element-loading-text="loadingText"
          element-loading-background="rgba(0,0,0,0.8)" :data="detail.dataList" stripe border>
          <el-table-column type="index" width="40px"></el-table-column>
          <el-table-column label="工序号" prop="sequence" width="80" show-overflow-tooltip></el-table-column>
          <el-table-column label="工序" prop="wsdescription" width="200" show-overflow-tooltip></el-table-column>
          <el-table-column label="内容描述" prop="workflowstepname" show-overflow-tooltip></el-table-column>
          <el-table-column label="工步" prop="description" width="200" show-overflow-tooltip></el-table-column>
          <el-table-column label="定额工时" prop="durationperunit" width="80" show-overflow-tooltip></el-table-column>
          <el-table-column label="状态" prop="status" width="100" show-overflow-tooltip></el-table-column>
          <el-table-column label="班组" prop="teamname" width="100" show-overflow-tooltip></el-table-column>
          <el-table-column label="派工时间" prop="dispatchdate" width="100" :formatter="splitDate"></el-table-column>
          <el-table-column label="派给工人" prop="workers" width="120" show-overflow-tooltip></el-table-column>
          <el-table-column label="派工人" prop="dispatchperson" width="80" show-overflow-tooltip></el-table-column>
          <el-table-column label="报工时间" prop="reportdate" width="100" :formatter="splitDate"></el-table-column>
          <el-table-column label="报工人" prop="reportperson" width="80" show-overflow-tooltip></el-table-column>
          <el-table-column label="检验时间" prop="checkdate" width="100" :formatter="splitDate"></el-table-column>
          <el-table-column label="检验人" prop="checkperson" width="80" show-overflow-tooltip></el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination @current-change="handleDetailCurrentChange" :current-page="detail.queryInfo.currentPage"
          :page-sizes="[detail.queryInfo.pageSize]" :page-size="detail.queryInfo.pageSize"
          layout="prev,pager,next,jumper,sizes,total" :total="detail.total">
        </el-pagination>
      </el-dialog>
    </div>
  </div>
  <script type="module">
    import { fetchSuggestion, exportExcelData, getParams } from '../Scripts/hooks/useCommonMethod.js'
    import { getUserInfo, getFactoryInfo, getFactoryListByCode } from '../Scripts/hooks/fetchAPI.js'
    import { prevWeek, thisWeek, nextWeek, prevMonth, thisMonth, nextMonth } from '../Scripts/hooks/useDateShortCut.js'
    import { splitDate } from '../Scripts/hooks/format.js'
    import Vue2ReportHeader from '../Scripts/components/Vue2ReportHeader.js'
    const app = new Vue({
      el: '#app',
      components: { Vue2ReportHeader },
      data: {
        splitDate,
        queryInfo: {
          factoryname: '',
          factoryid: '',
          engineno: '',
          mfgordername: '',
          workflowname: '',
          productfamilyname: '',
          creater: '',
          createdate: '',
          finishdate: '',
          status: '',
          currentPage: 1,//当前页
          pageSize: 12//每页显示条数
        },
        total: 0,
        loading: false,
        loadingText: '数据加载中...',
        dataList: [],
        detail: {
          dialogVisiable: false,
          queryInfo: { containerid: '', workflowid: '', currentPage: 1, pageSize: 12 },
          dataList: [], total: 0, title: ''
        },
        pickerOptions: { shortcuts: [prevWeek, thisWeek, nextWeek, prevMonth, thisMonth, nextMonth] },
        factoryList: []
      },
      methods: {
        // 查询发动机号
        getEngineNoAsync(value, callback) {
          fetchSuggestion({ actionName: 'getEngineNoAsync', name: 'engineno', value, callback })
        },
        // 查询订单号
        getMfgordernameAsync(value, callback) {
          fetchSuggestion({ actionName: 'getMfgordernameAsync', name: 'mfgordername', value, callback })
        },
        // 查询施工单号
        getSgdNoAsync(value, callback) {
          fetchSuggestion({ actionName: 'getSgdNoAsync', name: 'workflowname', value, callback })
        },
        // 设置进度条颜色
        customColorMethod(finishrate) {
          if (finishrate !== '100%') {
            return '#f56c6c';
          } else {
            return '#67c23a';
          }
        },
        //查询数据
        async getData(currentpage) {
          this.loading = true;
          this.loadingText = '数据加载中...';
          const { data: { data, meta, total } } = await axios.post('../handler/report/ZSWorkflownameReport.ashx?action=getData', {
            type: 'getData',
            ...this.queryInfo
          })
          this.loading = false;
          if (meta.status === 500) { return this.$message.error(meta.msg); }
          this.dataList = data;
          this.total = total;
        },
        // 查询明细
        showDetail(row) {
          this.detail.dialogVisiable = true;
          this.detail.title = `发动机:${row.engineno}--施工单:${row.workflowname}--各工序工步派报检明细`;
          this.detail.queryInfo.currentPage = 1;
          this.detail.queryInfo.containerid = row.containerid;
          this.detail.queryInfo.workflowid = row.workflowid;
          this.getDetail();
        },
        async getDetail() {
          this.loading = true;
          this.loadingText = '数据加载中...';
          const { data: { data, meta, total } } = await axios.post('../handler/report/ZSWorkflownameReport.ashx?action=getDetail', {
            type: 'getDetail',
            ...this.queryInfo,
            ...this.detail.queryInfo
          })

          this.loading = false;
          if (meta.status === 500) { return this.$message.error(meta.msg); }
          this.detail.dataList = data;
          this.detail.total = total;
        },
        handleCurrentChange(newPage) {
          this.queryInfo.currentPage = newPage;
          this.getData();
        },
        handleSizeChange(newSize) {
          this.queryInfo.pageSize = newSize;
          this.getData();
        },
        handleDetailCurrentChange(newPage) {
          this.detail.queryInfo.currentPage = newPage;
          this.getDetail();
        },
        // 导出excel
        async getExportExcel() {
          const config = {
            url: '../handler/report/ZSWorkflownameReport.ashx?action=exportExcel',
            data: { type: 'exportExcel', ...this.queryInfo }
          }
          exportExcelData(config, this);
        },
        factorySelectChange(val) {
          if (!val) {
            this.queryInfo.factoryname = '';
            return;
          }
          const filter = this.factoryList.filter(i => i.factoryid === val);
          if (filter.length > 0) {
            this.queryInfo.factoryname = filter[0].factoryname;
          }
        }
      },
      async mounted() {
        const startOfMonth = dayjs().startOf('month').format('YYYY-MM-DD');
        const endOfMonth = dayjs().endOf('month').format('YYYY-MM-DD');
        this.queryInfo.createdate = [startOfMonth, endOfMonth];

        const factoryid = Decrypt(getParams('factoryid'));
        const [factoryInfo, factoryList] = await Promise.all([
          getFactoryInfo({ factoryid, isMapping: '是' }),
          getFactoryListByCode({ factoryCode: '603|615|619' })//装配、修理、试车厂
        ]);

        this.factoryList = factoryList;
        const { factoryname } = factoryInfo[0];
        const userInfo = await getUserInfo({ employeeid: getParams("uid") });
        if (userInfo && factoryList.some(i => i.factoryid === userInfo.factoryid)) {
          this.queryInfo.factoryname = userInfo.factoryname;
          this.queryInfo.factoryid = userInfo.factoryid;
        } else {
          this.queryInfo.factoryname = factoryname;
          this.queryInfo.factoryid = factoryid;
        }
      },
      watch: {
        queryInfo: { handler() { this.queryInfo.currentPage = 1 }, deep: true }
      }
    })
  </script>
</body>

</html>